# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:android)

platform :android do
  def with_retries(times: 3, sleep_time: 10)
    tries = 0
    begin
      yield
    rescue => e
      tries += 1
      if tries < times
        UI.error("Retrying due to error: #{e}")
        sleep(sleep_time)
        retry
      else
        raise
      end
    end
  end

  desc "Upload App to Play Store Internal"
  lane :beta do |options|
    if options[:official]
      upload_to_play_store(
        package_name: 'chat.rocket.android',
        track: 'internal',
        aab: 'app/build/outputs/bundle/officialRelease/app-official-release.aab'
      )
    else
      upload_to_play_store(
        package_name: 'chat.rocket.reactnative',
        track: 'internal',
        aab: 'app/build/outputs/bundle/experimentalRelease/app-experimental-release.aab'
      )
    end
  end

  desc "Upload App to Internal App Sharing"
  lane :internal_app_sharing do |options|
    if options[:official]
      upload_to_play_store_internal_app_sharing(
        package_name: 'chat.rocket.android',
        aab: 'app/build/outputs/bundle/officialRelease/app-official-release.aab'
      )
    else
      upload_to_play_store_internal_app_sharing(
        package_name: 'chat.rocket.reactnative',
        aab: 'app/build/outputs/bundle/experimentalRelease/app-experimental-release.aab'
      )
    end
  end

  desc "Upload App to Play Store Production"
  lane :experimental_production do
    upload_to_play_store(
      package_name: 'chat.rocket.reactnative',
      track: 'production',
      aab: 'app/build/outputs/bundle/experimentalRelease/app-experimental-release.aab'
    )
  end

  desc "Upload App to Play Store Open Testing"
  lane :official_open_testing do
    upload_to_play_store(
      package_name: 'chat.rocket.android',
      track: 'beta',
      aab: 'app/build/outputs/bundle/officialRelease/app-official-release.aab'
    )
  end
end
